package a;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/ServletRequestDemo5")
public class ServletRequestDemo5 extends HttpServlet {
    //所有的servlet资源默认都是get提交方式
    //get

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    //解决post请求产生乱码问题(只要post请求就会产生乱码)
    //出现乱码的地方 在 Tomcat原来的编码是ISO8859-1  request获取
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收请求参数之前，需要先解决乱码
        //方式一
        //request.setCharacterEncoding("UTF-8");
        //2.接受请求参数
        String username = request.getParameter("username");
        System.out.println(username);
        //方式二
        username = new String(username.getBytes("ISO8859-1"),"UTF-8");
        System.out.println(username);
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("GBK");    --   妄图改页面乱码
        response.getWriter().write(request.getParameter("username"));

    }
}
